<template>
	<div class="system-dept-container">
		<!-- 查询条件-->
		<el-card shadow="hover">
			<el-form inline v-model="state.params" size="small">
				<el-form-item>
					<el-input v-model="state.params.clientKey" placeholder="搜索客户端Key"></el-input>
				</el-form-item>
				<el-form-item>
					<el-select v-model="state.params.resourceIds" placeholder="搜索可访问资源" clearable>
						<el-option label="公共模块" value="common" />
						<el-option label="oauth2-client" value="oauth2-resource" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="state.params.grantTypes" placeholder="搜索授权类型" clearable>
						<el-option label="授权码登录" value="authorization_code" />
						<el-option label="账号密码登录" value="password" />
						<el-option label="客户端赠书登录" value="client_credentials" />
						<el-option label="短信登录" value="sms" />
						<el-option label="是否可刷新" value="refresh_token" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="state.params.scopes" placeholder="搜索授权类型" clearable>
						<el-option label="所有" value="all" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" class="ml10" @click="initTableData">
						<el-icon>
							<elementSearch />
						</el-icon>
						查询
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<!--分割线-->
		<el-divider></el-divider>
		<!--数据展示和分页-->
		<el-card shadow="never">
			<template #header>
				<div class="card-header">
					<span>认证客户端列表</span>
					<!--新增等操作区域-->
					<el-button size="small" type="success" class="ml10" @click="openAddWindow">
						<el-icon>
							<elementDocumentAdd />
						</el-icon>
						新增认证客户端
					</el-button>
				</div>
			</template>
			<!--表格展示区-->
			<el-table :data="state.tableData.data" style="width: 100%">
        <el-table-column align="center" label="客户端名称" prop="clientName" show-overflow-tooltip />
				<el-table-column align="center" label="客户端key" prop="clientKey" show-overflow-tooltip />
				<el-table-column align="center" label="客户端密匙" prop="clientSecret" show-overflow-tooltip />
				<el-table-column align="center" label="可访问资源" prop="resourceIds" show-overflow-tooltip />
				<el-table-column align="center" label="授权方式" prop="grantTypes" show-overflow-tooltip width="500">
					<template #default="scope">
						<el-tag :key="tag" v-for="tag in scope.row.grantTypes">
							{{ tag }}
						</el-tag>
					</template>
				</el-table-column>
				<el-table-column align="center" label="访问域" prop="scopes" show-overflow-tooltip />
				<el-table-column align="center" label="授权之后回调地址" prop="redirectUris" show-overflow-tooltip />
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="mini" type="text" @click="openEditWindow(scope.row)">修改</el-button>
						<el-popconfirm
							confirm-button-text="删除"
							cancel-button-text="取消"
							icon-color="red"
							title="确定删除这条记录吗？"
							@confirm="delRow(scope.row)"
						>
							<template #reference>
								<el-button size="mini" type="text">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</el-table>
			<!--分页-->
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.params.pageNo"
				background
				v-model:page-size="state.params.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>

		<AddOauthClient ref="addRef" @initTableData="initTableData" />
		<EditOauthClient ref="editRef" @initTableData="initTableData" />
	</div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue';
import { delOauthClient, getOauthClientList } from '/@/api/tenant/oauthClient';
import AddOauthClient from '/@/views/tenant/oauthClient/component/addOauthClient.vue';
import EditOauthClient from '/@/views/tenant/oauthClient/component/editOauthClient.vue';
import { useRoute } from 'vue-router';

const addRef = ref();
const editRef = ref();
const route = useRoute();

const state = reactive({
	tenantId: route.query.tenantId,
	tableData: {
		data: [],
		total: 0,
		loading: false,
	},
	params: {
		pageNo: 1,
		pageSize: 10,
		clientKey: '',
		resourceIds: '',
		grantTypes: '',
		scopes: '',
		platform: true,
	},
});
// 初始化表格数据
const initTableData = () => {
	state.params.platform = state.tenantId == null || state.tenantId == '';
	getOauthClientList(state.params).then((res) => {
		state.tableData.data = res.data;
		state.tableData.total = res.extra.total;
	});
};

//分页大小改变
const onHandleSizeChange = (val: number) => {
	state.params.pageSize = val;
	initTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.params.pageNo = val;
	initTableData();
};

// 打开新增菜单弹窗
const openAddWindow = () => {
	addRef.value.openWindows(state.tenantId);
};

const openEditWindow = (row: any) => {
	editRef.value.openWindows(row.clientId, state.tenantId);
};

const delRow = (row: any) => {
	delOauthClient(row.clientId).then(() => {
		initTableData();
	});
};

// 页面加载时
onMounted(() => {
	initTableData();
});
</script>

<style scoped></style>
